package gui.threads;

import gui.Main;
import gui.Search;
import gui.Utils;

import java.sql.ResultSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import LogicSide.SearchQuery;
import LogicSide.Session;
import LogicSide.client;

public class SearchThread extends Thread {

	private String songName;
	private String artistName;
	private String albumName;
	private String genreName;
	private Shell shell;


	public SearchThread(String songName, String artistName, String albumName,
			String genreName, Shell shell) {
		super();
		this.songName = songName;
		this.albumName = albumName;
		this.artistName = artistName;
		this.genreName = genreName;
		this.shell = shell;

	}

	@Override
	public void run() {
		if (SearchQuery.checkFields(songName, artistName, albumName, genreName)) {
			final ResultSet resultSet = SearchQuery.execute(Main.session, albumName, artistName, genreName, songName);
			Main.display.asyncExec(new Runnable() {
				
				@Override
				public void run() {
					Utils.regularCursor(Main.display, shell);
					if(Main.inSearch){
					// remove current displaying panel
					Main.current.dispose();
					// update current displaying panel
					Main.current = Search.createSearch(shell, resultSet);
					//needed for it to work
					shell.layout(true, true);
					}
				}
			});

		} else {
			Main.display.asyncExec(new Runnable() {
				
				@Override
				public void run() {
					Utils.regularCursor(Main.display, shell);
					Utils.popUpBox("Incorrect fields",
							"At least one search field must be filled", shell);
				}
			});

		}
	}

}
